#!/bin/sh

for arg in $@; do 
	if [ "x$arg" = "x--help" ]; then
		echo "Usage: $0 [OPTION]... [ITERATIONS] [TESTGROUP]..."
		echo ""
		echo "Works the same as test-eglib or test-glib with the following"
		echo "exception. Run test-eglib --help for details on normal testing"
		echo ""
		echo "If the first OPTION is --speed-compare, the following is"
		echo "applicable to this program:"
		echo ""
		echo "  --speed-compare    run drivers in -qtni mode and report"
		echo "                     speed comparison information"
		echo ""
		echo "After --speed-compare, the number of iterations "
		echo "(optional, default is 100000) can be specified, followed "
		echo "by specific tests to run (or none to run all)"
		echo ""
		echo "If --speed-compare is not the first argument, all arguments are"
		echo "passed on directly to each driver"
		echo ""
		exit 1
	fi
done

if [ ! -x "./test-glib" -o ! -x "./test-eglib" ]; then
	make
fi

if [ "x$1" = "x--speed-compare" ]; then
	ITERATIONS=100000
	if [ ! -z "$2" ]; then
		case $2 in
			*[0-9]*) ITERATIONS=$2; break;
		esac
	fi	

	OPTIONS="-qnti $ITERATIONS"

	for arg in $@; do
		if [ "x$arg" = "x--speed-compare" ]; then	
			continue;
		elif [ "$arg" = "$ITERATIONS" ]; then
			continue;
		fi

		OPTIONS="$OPTIONS $arg"
	done
	
	echo "Running tests with $OPTIONS..."
	
	GLIB=`./test-glib $OPTIONS`
	EGLIB=`./test-eglib $OPTIONS`

	# this blows
	FASTER_NAME=`echo "$GLIB GLib $EGLIB EGlib" | awk '{ if($1 < $3) print $2; else print $4 }'`
	FASTER_SPEED=`echo "$GLIB $EGLIB" | awk '{ if($1 < $2) print $1; else print $2 }'`
	SLOWER_NAME=`echo "$GLIB GLib $EGLIB EGlib" | awk '{ if($1 > $3) print $2; else print $4 }'`
	SLOWER_SPEED=`echo "$GLIB $EGLIB" | awk '{ if($1 > $2) print $1; else print $2 }'`

	FASTER_PERCENTAGE=`echo "$SLOWER_SPEED $FASTER_SPEED" | awk '{ print ($1 / $2) * 100 }'`

	echo "$FASTER_NAME $FASTER_SPEED"
	echo "$SLOWER_NAME $SLOWER_SPEED"
	echo "------------------------------------------------"
	echo "$FASTER_NAME is $FASTER_PERCENTAGE% faster than $SLOWER_NAME"
	
	exit 0;
fi

./test-eglib $@
./test-glib $@

